using NUnit.Framework;
using ValidationFramework.Reflection;
using ValidationFramework;

namespace ExampleLibraryCSharp
{
    public class AddWithTypeCacheExample
    {
        #region Example
        public class Person
        {
            private readonly MemberValidationManager validationManager;


            public Person()
            {
                validationManager = ValidationManagerFactory.GetPropertyValidationManager(this);
            }

            public int Age
            {
                get;
                set;
            }



            public bool Validate()
            {
                validationManager.Validate("Age");
                return validationManager.IsValid;
            }
        }

        [TestFixture]
        public class ExampleUsage
        {

            [Test]
            public void Run()
            {
                var personType = typeof(Person);
                var typeDescriptor = TypeCache.GetType(personType.TypeHandle);
                var propertyDescriptor = typeDescriptor.GetOrCreatePropertyDescriptor("Age");
                var rule = new RequiredRule<int>();
                propertyDescriptor.Rules.Add(rule);

                var person = new Person();
                Assert.IsFalse(person.Validate());
                person.Age = 10;
                Assert.IsTrue(person.Validate());
            }
        }
        #endregion
    }

}